package com.xn.myyxcampus;

import com.xn.myyxcampus.http.NetWorkHelper;
import com.xn.myyxcampus.utils.ActivityUtils;
import com.xn.myyxcampus.utils.AnimUtils;
import com.xn.myyxcampus.utils.ToastUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.animation.AnimationUtils;

public class LoadingActivity extends Activity {

	private boolean isNetWorkConnection = false;

	// 默认等待时间为2秒，登录成功后登录时间为0.5秒
	private int loadTime = 2000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loading);
		Intent intent = getIntent();
		try {
			loadTime = intent.getIntExtra("loadTime", 2000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		thread.start();
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (0 == msg.what) {
				ToastUtils.showToast("亲！还没有联网哦~~~");
			}
			Intent intent = new Intent(LoadingActivity.this, MainActivity.class);
			startActivity(intent);
			ActivityUtils.addToActivityStack(LoadingActivity.this);
			AnimUtils.scaleIn(LoadingActivity.this);
			finish();
		}
	};

	Thread thread = new Thread(new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			isNetWorkConnection = NetWorkHelper.isNetWorkConnection();
			try {
				Thread.sleep(loadTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Message msg = new Message();
			if (isNetWorkConnection) {
				msg.what = 1;// 处于联网状态
			} else {
				msg.what = 0;// 处于断网状态
			}
			handler.sendMessage(msg);
		}
	});
}
